package com.sx.pipeline.ui.bind

import android.text.TextUtils
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.gyf.immersionbar.ImmersionBar
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.app.config.IntentKey
import com.sx.pipeline.app.config.SmsCodeType
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActBindPhoneBinding

/**
 * @Author: YY
 * @Date: 2022/4/14
 * @Description: 忘记密码
 */
@Route(path = RoutePath.Login.BIND_PHONE)
class BindPhoneActivity : CYDataBindingBaseActivity<ActBindPhoneBinding, BindPhoneVM>
    (R.layout.act_bind_phone), View.OnClickListener {

    @JvmField
    @Autowired(name = IntentKey.DATA)
    var openId: String? = ""

    @JvmField
    @Autowired(name = IntentKey.TYPE)
    var type: Int = 0

    override fun initStatusBar() {
        ImmersionBar.with(this).statusBarDarkFont(true)
            .fitsSystemWindows(false).init()
    }

    override fun initData() {
        super.initData()
        mBinding.setVariable(BR.onClick, this)
    }

    override fun initViewObservable() {
        super.initViewObservable()

        observe(mViewModel.smsCodeEvent) {
            mBinding.tvCountDown.start()
        }

        observe(mViewModel.modifyEvent) {
            finish()
        }
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.image_left_gray -> {
                finish()
            }
            R.id.tv_count_down -> {
                val phone = mBinding.etPhone.text.toString()
                mViewModel.sendSmsCode(phone, SmsCodeType.BIND_PHONE.code)
            }
            R.id.tv_confirm -> {
                val phone = mBinding.etPhone.text.toString()
                val smsCode = mBinding.etVerifyCode.text.toString()
                if (!TextUtils.isEmpty(openId)) {
                    mViewModel.bindPhone(openId!!, phone, smsCode, type)
                }
            }
        }
    }


}